package com.example.demo.util;

import org.junit.Test;

/**
 * @author wangfei
 */
public class StringTools {
    public  static String rightPad(String str, int size, char  padChar) {
        return null;
    }

    public static String rightPad(String str, int size, String padStr) {
        if(str == null) {
            return null;
        }else {
            if(isEmpty(padStr)){
                padStr = " ";
            }

            // 填充字符长度
            int padLen = padStr.length();
            // 实际数据长度
            int strLen = str.length();
            // 需要填充的长度
            int pads = size - strLen;
            if(pads <= 0){
                return str;
            } else if(padLen ==1 && pads <= 8192) {
                return rightPad(str, size, padStr.charAt(0));
            } else if( pads == padLen){
                return str.concat(padStr);
            } else if( pads < padLen) {
                return str.concat(padStr.substring(0, pads));
            } else {
                char[] padding = new char[pads];
                char[] padChars = padStr.toCharArray();

                for(int i = 0; i < pads; ++i) {
                    padding[i] = padChars[i % padLen];
                }
                return str.concat(new String(padding));
            }
        }
    }

    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    @Test
    public void test(){
        String str = "wangfei";
        String padStr = "1";
        int size = 5;
        System.out.println(rightPad(str, size, padStr));
    }
}
